<?php
namespace Home\Controller;
use Think\Controller;

header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE '); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

class AuthenController extends Controller {
    /*
     * 用户登录
     */
   public function login(){
       if(IS_GET){
           $this->display();
       }else{
           $where['nickname']=I('post.nickname');
           $where['password']=md5(I('post.password'));
           $info=M('user')->where($where)->find();
           if($info){
               session('index_user',$info);
               $this->redirect('Myself/index');
           }else{
               $this->error('账号或密码错误！');
           }
       }

   }
   /*
    * 注销
    */
   public function logout(){
       session('index_user',null);
       $this->redirect('login');
   }

   /*
    * 用户注册
    */
   public function regist(){
       if(IS_GET){
           $this->display();
       }else{
            $data=I('post.');
            $user=M('user');
           $map['email'] = $data['email'];
            if($exist_info=$user->where($map)->select()){
              $this->error('该邮箱已被注册');
            }else{
              $data['password']=md5($data['password']);
              $data['create_time']=time();
              $info=M('user')->add($data);
              if($info){
                  $data['last_time']=0;
                  $data['id']=$info;//$info为主键id
                  $data['usercard']=0;
                  if($data['role'] ==2){
                      //员工
                      $this->success('注册成功,等待管理员审核',U('login'));
                  }else{
                      //用户
                      session('index_user',$data);
                      $this->success('注册成功',U('Myself/index'));
                  }

              }
              else
                  $this->error('注册失败');
          }           
       }
   }
}